/*******************************************************************************
 * Level Master 2000
 * Copyright (C) 2010 Christian Herzog
 * For more information see http://code.google.com/p/levelmaster2000/
 *
 * This file is licensed under the terms of the MIT license, see
 * http://www.opensource.org/licenses/mit-license.php for further information.
 ******************************************************************************/
package com.zog
{
	import com.pblabs.engine.PBE;
    import com.pblabs.engine.core.ObjectType;
    import com.pblabs.engine.core.ObjectTypeManager;
    import com.pblabs.engine.debug.Logger;
    import com.pblabs.engine.debug.Profiler;
    import com.pblabs.engine.entity.EntityComponent;
        import com.pblabs.rendering2D.SpriteSheetRenderer;
    import com.pblabs.rendering2D.spritesheet.SpriteSheetComponent;
    import com.pblabs.rendering2D.SimpleSpatialComponent;
    import com.pblabs.rendering2D.SpriteRenderer;
    import com.pblabs.engine.entity.*;
    import flash.events.Event;
    import flash.geom.Point;
    import flash.geom.Rectangle;
    import com.pblabs.box2D.Box2DSpatialComponent;
    import com.pblabs.box2D.Box2DManagerComponent;
    import com.pblabs.box2D.PolygonCollisionShape;
    import com.pblabs.box2D.CollisionEvent;
    
    public class SimplePlatform extends EntityComponent
    {
        public function get position():Point
        {
            if(spatialComp)
            {
                return spatialComp.position;
            }
            else
            {
                Logger.error(this, "set position", "Add component first!");
                return new Point();
            }
        }

        public function set position(value:Point):void
        {
            if(spatialComp)
            {
                spatialComp.position = value;
            }
            else
            {
                Logger.error(this, "set position", "Add component first!");
            }
        }
        
        public function get rotation():Number
        {
            if(spatialComp)
            {
                return spatialComp.rotation;
            }
            else
            {
                Logger.error(this, "set rotation", "Add component first!");
                return 0.0;
            }
        }

        public function set rotation(value:Number):void
        {
            if(spatialComp)
            {
                spatialComp.rotation = value;
            }
            else
            {
                Logger.error(this, "set rotation", "Add component first!");
            }
        }        
        
        
        //Privates:
        private var spatialComp:Box2DSpatialComponent;
        private var renderComp:SpriteRenderer;
        private var shape:PolygonCollisionShape;
        

        protected override function onAdd():void
        {
            super.onAdd();

            //Ugly hack to work when created per AS as well as per deserialization:
            if(!owner.lookupComponentByName("Render"))
            {
                //Create our spatial component / body:
                spatialComp = new Box2DSpatialComponent();
                spatialComp.spatialManager= PBE.spatialManager as Box2DManagerComponent;
                spatialComp.position = new Point(0, 0);
                spatialComp.size = new Point(256, 64);
                spatialComp.collisionType = new ObjectType("Platform");
                spatialComp.canMove = false;
                spatialComp.canRotate = false;
                spatialComp.canSleep = true;
                spatialComp.collidesWithTypes = new ObjectType("Player", "HurtObject");
                
                //spatialComp.body.
                
                //We also need a collision shape, which collides with the world (meaning other shapes):
                shape = new PolygonCollisionShape();
                shape.friction = 1.0;
                shape.vertices = new Array();
                shape.vertices.push( new Point( -1, -1) );
                shape.vertices.push( new Point( 1, -1) );
                shape.vertices.push( new Point( 1, 1) );
                shape.vertices.push( new Point( -1, 1) );
                
                 
                //Now we add the shape to the spatial component / body:
                spatialComp.collisionShapes = new Array();
                spatialComp.collisionShapes.push(shape);
                 
                //Add the component to the entity:
                owner.addComponent( spatialComp, "Spatial" ); 			

                //don't forget to build the collision shapes after adding component:		
                spatialComp.buildCollisionShapes(); 
                
                
                //Since we want to visualize the entity, we need a simple sprite renderer:
                renderComp = new SpriteRenderer(); 

                renderComp.scene = PBE.scene;
                renderComp.fileName = "../assets/Images/platformDirt.png";
                renderComp.positionProperty  = new PropertyReference("@Spatial.position");
                renderComp.rotationProperty = new PropertyReference("@Spatial.rotation");

                //Add the component to the entity:
                owner.addComponent( renderComp, "Render" )
            }
            else
            {
                spatialComp = owner.lookupComponentByType(Box2DSpatialComponent) as Box2DSpatialComponent;
                renderComp = owner.lookupComponentByType(SpriteRenderer) as SpriteRenderer;
            }
            
        }
        
    	
		protected override function onRemove():void
		{
			super.onRemove();
		}
    }
}